package com.wongoing.sys.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wongoing.base.PageResult;
import com.wongoing.sys.model.ext.SysLogExt;
import com.wongoing.sys.service.SysLogService;
import com.wongoing.util.LogService;
import com.wongoing.util.ModelAndViewConstants;
import com.wongoing.util.PageUtil;

@Controller
@RequestMapping("/sys/sysLogAction")
public class SysLogAction {
	
	private SysLogService service;
	
	@Autowired
	public void setService(SysLogService service) {
		this.service = service;
	}
	/**
	 * 功能说明：进入列表显示界面
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月15日 下午12:38:04
	 * @param pageSize 每页记录数
	 * @param pageCurrent 当前页索引
	 * @param orderField 排序字段
	 * @param orderDirection 排序方向
	 * @param paramAccount 参数：账户
	 * @param paramLocal 参数：区域
	 * @param paramIp 参数：IP
	 * @param paramEventName 参数：事件名称
	 * @param paramEventRecord 参数：时间记录
	 * @param paramLogDate 参数：日志日期
	 * @param paramType 参数：类型
	 * @return
	 */
	@RequestMapping(value = "/toList")
	public ModelAndView toList(Integer pageSize, Integer pageCurrent, String orderField, String orderDirection,
			String paramAccount, String paramLocal,String paramIp,String paramEventName,String paramEventRecord,
			String paramLogDate,String paramType){
		ModelAndView mv = null;
		try {
			PageResult<SysLogExt> pageResult = PageUtil.pageSet(this.getClass(), pageSize, pageCurrent, orderField, orderDirection);
			//模糊查询判断
			List<Boolean> multiFuzzyQuery = new ArrayList<Boolean>();
	
			if (paramType != null && !"".equals(paramType)) {
				pageResult.getParam().put("type", paramType);
				pageResult.setOrderField("t1.Type");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramType", paramType);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			
			if (paramLogDate != null && !"".equals(paramLogDate)) {
				pageResult.getParam().put("logDate", paramLogDate);
				pageResult.setOrderField("t1.LogDate");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramLogDate", paramLogDate);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if (paramEventRecord != null && !"".equals(paramEventRecord)) {
				pageResult.getParam().put("eventRecord", paramEventRecord);
				pageResult.setOrderField("t1.EventRecord");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramEventRecord", paramEventRecord);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if (paramEventName != null && !"".equals(paramEventName)) {
				pageResult.getParam().put("eventName", paramEventName);
				pageResult.setOrderField("t1.EventName");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramEventName", paramEventName);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			
			if (paramIp != null && !"".equals(paramIp)) {
				pageResult.getParam().put("ip", paramIp);
				pageResult.setOrderField("t1.Ip");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramIp", paramIp);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			
			if (paramLocal != null && !"".equals(paramLocal)) {
				pageResult.getParam().put("local", paramLocal);
				pageResult.setOrderField("t1.Local");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramLocal", paramLocal);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			
			if (paramAccount != null && !"".equals(paramAccount)) {
				pageResult.getParam().put("account", paramAccount);
				pageResult.setOrderField("t2.Account");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramAccount", paramAccount);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if(PageUtil.multiFuzzyQuery(multiFuzzyQuery)){
				pageResult.setOrderField("t1.Id");
				pageResult.setOrderDirection("ASC");
			}
			pageResult = this.service.getLogListPageResult(pageResult);
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.PAGE_RESULT, pageResult);
			mv.setViewName(ModelAndViewConstants.LOG_MAIN_VIEW);
			LogService.getInstance(this).debug(ModelAndViewConstants.LOG_MAIN_VIEW);
		} catch (Exception ex) {
			LogService.getInstance(this).error("分页获取日志列表失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
}
